package com.mspbots.cw.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * com.mspbots.cw.config.RabbitConfiguration
 *
 * @author Jason
 * @date 2019/12/16
 **/
@Configuration
@RequiredArgsConstructor
public class RabbitConfiguration {

    private final AmqpAdmin amqpAdmin;

    @Bean
    public void declareTimeEntry() {

        Exchange timeEntryExchange = ExchangeBuilder.fanoutExchange("msp.trigger.timeentry").build();
        this.amqpAdmin.declareExchange(timeEntryExchange);

        Queue wiseForwardQueue = QueueBuilder.durable("trigger.intforward").build();
        this.amqpAdmin.declareQueue(wiseForwardQueue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(wiseForwardQueue)
                .to(timeEntryExchange).with("trigger.entries").noargs());


        Exchange exchange = ExchangeBuilder.fanoutExchange("msp.attendance.sheet.times").build();
        this.amqpAdmin.declareExchange(exchange);

        Queue queue = QueueBuilder.durable("attendance.sheet.TsheetInCheck").build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with("sheet.TsheetInCheck").noargs());

    }

    @Bean
    public void declareTicket() {

        Exchange ticketExchange = ExchangeBuilder.fanoutExchange("msp.wise.callback.tickets").build();
        this.amqpAdmin.declareExchange(ticketExchange);

        Queue ticketForwardQueue = QueueBuilder.durable("ticket.forward").build();
        this.amqpAdmin.declareQueue(ticketForwardQueue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(ticketForwardQueue)
                .to(ticketExchange).with("trigger.tickets").noargs());

        Exchange triggerExchange = ExchangeBuilder.fanoutExchange("msp.trigger.tickets").build();
        this.amqpAdmin.declareExchange(triggerExchange);

    }

    @Bean
    public MessageConverter rabbitMessageConverter(ObjectMapper objectMapper) {
        return new Jackson2JsonMessageConverter(objectMapper);
    }
}
